package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    @CacheEvict(value = RedisConstant.SETMEAL, allEntries = true)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        }
        setmealDishMapper.saveDish(setmealDishes);


    }

    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @CacheEvict(value = RedisConstant.SETMEAL, allEntries = true)
    public void delete(List<Long> ids) {
        int count = setmealMapper.querySetmealStatus(ids);
        if (count > 0) {
            throw new BaseException("套餐正在售卖中");
        }
        setmealMapper.delete(ids);
        setmealDishMapper.deleteDish(ids);
    }

    @Override
    @Transactional
    public SetmealVO findById(Long setmealId) {
        SetmealVO setmealVO = setmealMapper.finById(setmealId);
        setmealVO.setSetmealDishes(setmealDishMapper.findByDishId(setmealId));
        return setmealVO;
    }

    @Override
    @CacheEvict(value = RedisConstant.SETMEAL, allEntries = true)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        setmealDishMapper.deleteDish(List.of(setmeal.getId()));

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        }
        setmealDishMapper.saveDish(setmealDishes);
    }

    @Override
    @CacheEvict(value = RedisConstant.SETMEAL, allEntries = true)
    public void updateStatus(Integer id, Integer status) {
        setmealMapper.updateStatus(id, status);
    }
}
